 <?php
class Htmltemplates extends Model_Abstract
{
	public $tableName = 'htmltemplates';
	
	
	protected $_validation = array(
		'name' => array(
			array('max_length', '模板名称长度不能超过100', 100),
			array('not_empty', '请填写模板名称')
		),
		'description' => array(
			array('max_length', '请在5000个字以内描述模板', 5000),
		),
		'root_foldername' => array(
			array('max_length', '资源路径最长100个字符', 100),
		),
		'thumb_small' => array(
			array('max_length', '预览图名称（小）最长50个字符', 50),
			array('not_empty', '请放置一张预览图（小）')
		),
		'thumb_big' => array(
			array('max_length', '预览图名称（大）最长50个字符', 50),
			array('not_empty', '请放置一张预览图（大）')
		),
		'preview_indexpage' => array(
			array('max_length', '首页文件名最长50个字符', 50),
			array('not_empty', '请设置一个首页预览文件')
		),
		'download_pack' => array(
			array('max_length', '下载包文件名最长50个字符', 50),
			array('not_empty', '请提供下载包')
		),
		'type_area_id' => array(
			array('is_int', 'type_area_id必须是数字'),
		),
		'type_color_id' => array(
			array('is_int', 'type_color_id必须是数字'),
		),
		'type_columnwidth_id' => array(
			array('is_int', 'type_columnwidth_id必须是数字'),
		),
		'type_sidebarcorner_id' => array(
			array('is_int', 'type_sidebarcorner_id必须是数字'),
		),
		'type_using_id' => array(
			array('is_int', 'type_using_id必须是数字'),
		),
		'view_num' => array(
			array('is_int', 'view_num必须是数字'),
		),
		'download_num' => array(
			array('is_int', 'download_num必须是数字'),
		),
	);
	
	public function init()
	{
		$this->initAttributes();
	}
	
	public static function model($classname = __CLASS__)
	{
		return parent::model($classname);
	}
	
	public function attributeLabels()
	{
		return array(
			'name' => '模板名',
			'description' => '模板描述'
		);
	}
	
	public function mergeTypeWithId($types, $htmltemplates)
	{
		foreach($htmltemplates as $k => $ht){
			$htmltemplates[$k]['type_area'] = '';
			$htmltemplates[$k]['type_area_en'] = '';
			$htmltemplates[$k]['type_color'] = '';
			$htmltemplates[$k]['type_color_en'] = '';
			$htmltemplates[$k]['type_columnwidth'] = '';
			$htmltemplates[$k]['type_columnwidth_en'] = '';
			$htmltemplates[$k]['type_sidebarcorner'] = '';
			$htmltemplates[$k]['type_sidebarcorner_en'] = '';
			$htmltemplates[$k]['type_using'] = '';
			$htmltemplates[$k]['type_using_en'] = '';
		
			if(!empty($types[$ht['type_area_id']])){
				$htmltemplates[$k]['type_area'] = $types[$ht['type_area_id']]['name'];
				$htmltemplates[$k]['type_area_en'] = $types[$ht['type_area_id']]['name_en'];
			}
			if(!empty($types[$ht['type_color_id']])){
				$htmltemplates[$k]['type_color'] = $types[$ht['type_color_id']]['name'];
				$htmltemplates[$k]['type_color_en'] = $types[$ht['type_color_id']]['name_en'];
			}
			if(!empty($types[$ht['type_columnwidth_id']])){
				$htmltemplates[$k]['type_columnwidth'] = $types[$ht['type_columnwidth_id']]['name'];
				$htmltemplates[$k]['type_columnwidth_en'] = $types[$ht['type_columnwidth_id']]['name_en'];
			}
			if(!empty($types[$ht['type_sidebarcorner_id']])){
				$htmltemplates[$k]['type_sidebarcorner'] = $types[$ht['type_sidebarcorner_id']]['name'];
				$htmltemplates[$k]['type_sidebarcorner_en'] = $types[$ht['type_sidebarcorner_id']]['name_en'];
			}
			if(!empty($types[$ht['type_using_id']])){
				$htmltemplates[$k]['type_using'] = $types[$ht['type_using_id']]['name'];
				$htmltemplates[$k]['type_using_en'] = $types[$ht['type_using_id']]['name_en'];
			}
		}
		return $htmltemplates;
	}
	
	public function getAll()
	{
		$types = Types::model()->find('*', array('tablename' => 'htmltemplates'), null, null, array('setResultKey' => 'id'));
		$htmltemplates = $this->find('*');
		
		return $this->mergeTypeWithId($types, $htmltemplates);
	}
	
	public function paginationGetAll($page, $filter = array())
	{
		$types = Types::model()->find('*', array('tablename' => 'htmltemplates'), null, null, array('setResultKey' => 'id'));
		
		$perPage = HTMLTEMPLATES_LIST_PERPAGE;
		$limit = " LIMIT " .($page-1)*$perPage . ", {$perPage}";
		
		$sqlWhere = '';
		$query = array();
		$vars = array();
		$searchMode = ' AND ';
		if(!empty($filter)){
			foreach($filter as $k => $v){
				if(is_array($v)){
					$query[] = $k . ' IN (' . implode(',', $v) . ')';
				}else{
					$query[] = $k . '=?';
					$vars[] = $v;
				}
			}
			$searchMode = empty($_GET['search_mode_all'])? ' OR ' : ' AND ';
			$sqlWhere = ' WHERE ' . implode($searchMode, $query);
		}
		
		$sqlCount = " SELECT COUNT(*) total_num FROM htmltemplates " . $sqlWhere;
		$result = $this->_connection->query($sqlCount, $vars);
		$totalCount = $result[0]['total_num'];
		
		$sql = " SELECT * FROM htmltemplates ht ";
		$sql .= $sqlWhere . $limit;
		$result = $this->_connection->query($sql, $vars);
		$result = $this->mergeTypeWithId($types, $result);
		
		$result['totalCount'] = $totalCount;
		return $result;
	}
	
	public function getAllRoot()
	{
		$sql = " SELECT root_foldername FROM htmltemplates ";
		$result = $this->_connection->query($sql);
		if(!empty($result)){
			foreach($result as $k => $v){
				$result[$k] = $v['root_foldername'];
			}
		}
		
		return $result;
	}
	
	public function getAllRootFolderOnDisk($filter = array())
	{
		$filter[] = '.svn';
		$htmlTemplateFolders = Util::traversalDirectoryFile(HTMLTEMPLATES_FOLDER, $filter, 2);
    	
    	$htmlTemplateFoldersInfo = array();
    	if(!empty($htmlTemplateFolders)){
    		foreach($htmlTemplateFolders as $folder => $inFolder){
    			if(is_array($inFolder)){
    				$htmlTemplateFoldersInfo[$folder] = Util::RootListLevel1($inFolder);
    			}
    		}
    	}
    	return $htmlTemplateFoldersInfo;
	}
	
	public function getUnaddedTemplates()
	{
    	$htmlTemplatesAddedRootFolders = $this->getAllRoot();
		$htmlTemplateUnadded = $this->getAllRootFolderOnDisk($htmlTemplatesAddedRootFolders);
    	
    	return $htmlTemplateUnadded;
	}
	
	public function getTemplateOnDiskByRootfolder($rootfolder)
	{
		$allTemplatesOnDisk = $this->getAllRootFolderOnDisk();
		return $allTemplatesOnDisk[$rootfolder];
	}
	
	public function fileClassify($files)
	{
		$class = array(
			'图像' => array('jpg', 'png', 'jpeg', 'bmp', 'gif'),
			'压缩包' => array('zip', 'rar'),
			'other' => array('css', 'log', 'js'),
		);
		$ret = array();
		if(!empty($files)){
			foreach($files as $file){
				$ext = explode('.', $file);
				$ext = end($ext);
				if(in_array($ext, $class['图像'])){
					$c = '图像';
				}else if(in_array($ext, $class['压缩包'])){
					$c = '压缩包';
				}else if(in_array($ext, $class['other'])){
					$c = 'other';
				}else{
					$c = $ext;
				}
				$ret[$c][] = $file;
			}
		}
		return $ret;
	}

	protected function _validate()
	{
		$ret = true;
		
		$ret2 = parent::_validate();
		
		return ($ret && $ret2);
	}
	
	public function initAttributes()
	{
		$this->attributes = array(
				'name' => '',
				'description' =>'',
				'root_foldername' => '',
				'thumb_small' => '',
				'thumb_big' => '',
				'preview_indexpage' => '',
				'download_pack' => '',
				'view_num' => '',
				'download_num' => '',
				'type_area_id' => '',
				'type_color_id' => '',
				'type_columnwidth_id' => '',
				'type_sidebarcorner_id' => '',
				'type_using_id' => '',
				'created_date' => '',
				'created_int' => ''
		);
	}

}